package cn.sdormitory.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.sdormitory.common.utils.SysUserUtils;
import cn.sdormitory.schoolActivity.Req.SchoolActivityParamReq;
import cn.sdormitory.schoolActivity.Req.SchoolActivityReq;
import cn.sdormitory.schoolActivity.dao.SchoolActivityDao;
import cn.sdormitory.schoolActivity.entity.SchoolActivity;
import cn.sdormitory.service.AppActivityService;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysUser;
import cn.sdormitory.workflow.entity.ViolationApply;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @className: AppActivityServiceImpl
 * @author: Alex
 * @date: 2024/12/17
 **/
@Service
public class AppActivityServiceImpl implements AppActivityService {
	
	@Resource
	private SchoolActivityDao schoolActivityDao;
	
	@Resource
	private SysUserDao sysUserDao;
	
	@Override
	public int createOrUpdateActivity(SchoolActivityReq schoolActivityReq) {
		SchoolActivity schoolActivity = new SchoolActivity();
		if (Objects.isNull(schoolActivityReq.getId())) {
			// 新增校园活动
			BeanUtil.copyProperties(schoolActivityReq, schoolActivity);
			
			List<String> photos = schoolActivityReq.getPhotos();
			String photoStr = String.join(",", photos);
			
			schoolActivity.setActivityPhotos(photoStr);
			schoolActivity.setCreateTime(new Date());
			schoolActivity.setCreateBy(SysUserUtils.getSysUser().getUsername());
			
			int insert = schoolActivityDao.insert(schoolActivity);
			return insert;
		}
		
		// 修改
		BeanUtil.copyProperties(schoolActivityReq, schoolActivity);
		
		List<String> photos = schoolActivityReq.getPhotos();
		String photoStr = String.join(",", photos);
		
		schoolActivity.setActivityPhotos(photoStr);
		schoolActivity.setModifyTime(new Date());
		schoolActivity.setModifyBy(SysUserUtils.getSysUser().getUsername());
		int update = schoolActivityDao.updateById(schoolActivity);
		return update;
	}
	
	@Override
	public IPage<SchoolActivity> pageList(SchoolActivityParamReq vo) {
		SysUser sysUser = sysUserDao.selectById(vo.getUserId());
		vo.setUserId(sysUser.getUsername());
		IPage<SchoolActivity> page = new Page<>(vo.getPageNum(), vo.getPageSize());
		return schoolActivityDao.getActivityPage(vo, page);
	}
	
	@Override
	public boolean deleteById(long id) {
		int i = schoolActivityDao.deleteById(id);
		if (i > 0) {
			return true;
		}
		return false;
	}
	
	@Override
	public SchoolActivity getActivityDetailById(Long id) {
		SchoolActivity schoolActivity = schoolActivityDao.getActivityById(id);
		return schoolActivity;
	}
}
